//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "common\fn_storeTrigger.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;
private ["_trigger","_instance_id","_instance_content","_mission_triggers","_jip_instance","_jip_objetcs"];
DEFAULT_PARAM(_trigger,0,objNull);
if (isnil QGVAR(STORING_OBJECT_BUSY)) then { GVAR(STORING_OBJECT_BUSY) = -1; };
waituntil {(GVAR(STORING_OBJECT_BUSY) < time)};
GVAR(STORING_OBJECT_BUSY) = time + 10;
_instance_id = [_trigger] call FFUNC(GetInstanceID);
_instance_content = OGETVAR(GVAR(mission_store),_instance_id,(call FFUNC(hashEmpty)));
_mission_triggers = [_instance_content, TMF_HK_MISSION_TRIGGERS,[],THIS_FILE] call FFUNC(hashGet);
if (_trigger in _mission_triggers) then {
	TRACE_1("Object already stored",_trigger);
} else {
	TRACE_1("Adding object to the store",ARR_3(_trigger,_instance_id,count _mission_triggers));
	_mission_triggers set [ count _mission_triggers, _trigger];
	_instance_content = [_instance_content, TMF_HK_MISSION_TRIGGERS,_mission_triggers] call FFUNC(HashSet);
	OSETVAR(GVAR(mission_store),_instance_id,_instance_content);
};
if (isServer) then {
	_jip_instance = [_instance_id] call FFUNC(getMissionInstance);
	_jip_objetcs = [_jip_instance, TMF_HK_MISSION_TRIGGERS,[],THIS_FILE] call FFUNC(hashGet);
	if (_trigger in _jip_triggers) then {
		TRACE_1("Object already jipped",_trigger);	
	} else {
		TRACE_1("Adding object to the JIP store",ARR_3(_trigger,_instance_id,count _jip_objetcs));
		_jip_objetcs set [ count _jip_objetcs, _trigger];
		_jip_instance = [_jip_instance, TMF_HK_MISSION_TRIGGERS,_jip_objetcs] call FFUNC(HashSet);
		[_instance_id,_jip_instance] call FFUNC(setMissionInstance);
	};
};
GVAR(STORING_OBJECT_BUSY) = -1;
TRACE_1("End",ARR_2(_instance_id, count _mission_triggers));
TMFPROFILERSTOP;
